package com.cloud.sysmana.ser.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import com.cloud.sysmana.entity.SysDepa;
import com.cloud.sysmana.entity.dao.ISysDepaDao;
import com.cloud.sysmana.ser.ISysDepaService;
import com.cloud.util.base.RetType;
import com.cloud.util.jsTree.JsTreeJson;
import com.cloud.util.jsTree.JsTreeState;
import com.cloud.util.jsTree.JsTreeUtil;
import com.cloud.util.jsTree.JsTreeUtil.IObjectToJsTree;

/**
 * 部门管理
 * @author gaof
 * @date 2016年12月20日
 */
@Service
@Transactional
public class SysDepaService implements ISysDepaService {

	@Resource(name="sysDepaDao")
	private ISysDepaDao sysDepaDao;
	
	private Integer lenth=0;
	/**
	 * 获取部门列表
	 * @return
	 */
	@Override
	public RetType depaList() {
		
		RetType retType = new RetType();
		
		//获取所有部门
		List<SysDepa> depaList = sysDepaDao.depaList();
		
		JsTreeJson jsTree = new JsTreeJson();
		jsTree.setId("0");//根节点
		jsTree.setText("部门管理");
		JsTreeState state = new JsTreeState();
		state.setOpened(true);
		jsTree.setState(state);
		
		jsTree = JsTreeUtil.getJsTree(jsTree, depaList, new IObjectToJsTree() {
			
			@Override
			public JsTreeJson objectToJsTree(Object obj) {
				SysDepa req = (SysDepa) obj;
				JsTreeJson tree = getJsTreeJson(req);
				return tree;
			}
			
			@Override
			public String getObjectPId(Object obj) {
				SysDepa req = (SysDepa) obj;
				return String.valueOf(req.getPid());
			}
		});
		
		retType.setErrCode(RetType.RET_SUCCESS);
		retType.setData(jsTree);
		
		return retType;
	}
	
	/**
	 * 需求转jsTree数据
	 * @param req
	 * @return
	 */
	protected JsTreeJson getJsTreeJson(SysDepa req) {
		
		JsTreeJson tree = new JsTreeJson();
		tree.setId(req.getDepaId().toString());
		tree.setText(req.getDepaName());
		//图标
		/*if(Constant.APP_REQ_FUNC_DET.equals(req.getType().toString())){//具体功能
			tree.setIcon("fa fa-file-text-o");
		}else if(Constant.APP_REQ_FUNC.equals(req.getType().toString())){//功能
			tree.setIcon("fa fa-list-ul");
		}else if(Constant.APP_REQ_MODEL.equals(req.getType().toString())){//模块
			tree.setIcon("fa fa-list-alt");
		}else{//其他
			tree.setIcon("fa fa-file");
		}*/
		//状态
		JsTreeState state = new JsTreeState();
		state.setOpened(true);
		tree.setState(state);
		//a标签附带参数
		tree.setA_attr(req);
		
		return tree;
	}

	/**
	 * 添加部门
	 * @param depa
	 * @return
	 */
	@Override
	public RetType addDepa(SysDepa depa) {
		
		RetType retType = null;
		
		if(depa.getDepaId() == null){//新增部门
			
			retType = addDepaDo(depa);
			
		}else{//编辑部门
			
			retType = updateDepaDo(depa);
		}
		
		return retType;
	}

	
	/**
	 * 更新部门
	 * @param depa
	 * @return
	 */
	private RetType updateDepaDo(SysDepa depa) {
		
		RetType retType = new RetType();
		
		SysDepa depaHis = sysDepaDao.getDepaById(depa.getDepaId());
		if(depaHis == null){
			retType.setErrCode(RetType.RET_FAILE);
			retType.setErrMsg("部门不存在，请刷新页面");
		}
		
		depaHis.setDepaName(depa.getDepaName());
		depaHis.setDepaCode(depa.getDepaCode());
		depaHis.setPid(depa.getPid());
		depaHis.setRemark(depa.getRemark());
		depaHis.setSeq(depa.getSeq());
		
		retType.setErrCode(RetType.RET_SUCCESS);
		
		return retType;
	}

	/**
	 * 新增部门
	 * @param depa
	 * @return
	 */
	private RetType addDepaDo(SysDepa depa) {
		
		depa.setUpdateTime(new Date());
		depa.setDepaType("1");
		depa.setStatus("1");
		sysDepaDao.addDepa(depa);
		
		RetType retType = new RetType();
		retType.setErrCode(RetType.RET_SUCCESS);
		
		return retType;
	}

	/**
	 * 删除部门及其下级部门
	 * @param depa
	 * @return
	 */
	@Override
	public RetType delDepa(int id) {
		
		RetType retType = new RetType();
		List<SysDepa> depaList = null;
		
		if(id <= 0){//从根目录删除
			
			sysDepaDao.delAllDepa();
			
		}else{
			
			SysDepa depa = sysDepaDao.getDepaById(id);//获取需要删除的部门信息
			if(depa == null){
				retType.setErrCode(RetType.RET_FAILE);
				retType.setErrMsg("部门不存在，请刷新页面！");
				return retType;
			}
			
			depaList = new ArrayList<SysDepa>();
			depaList.add(depa);
			depaList.addAll(getAllChildDepa(depa));//获取所有下级部门
			
			sysDepaDao.delDepaList(depaList);
		}
		
		retType.setErrCode(RetType.RET_SUCCESS);
		return retType;
	}

	/**
	 * 获取所有下级部门
	 * @param depa 
	 * @return
	 */
	private List<SysDepa> getAllChildDepa(SysDepa depa) {
		
		List<SysDepa> depaList = new ArrayList<SysDepa>();
		getChildDepa(depa, depaList);
		
		return depaList;
	}

	/**
	 * 获取下级部门
	 * @param depa
	 * @param depaList
	 */
	private void getChildDepa(SysDepa depa, List<SysDepa> depaList) {
		
		List<SysDepa> childList = sysDepaDao.listDepaByPId(depa.getDepaId());
		if(childList != null && !childList.isEmpty()){
			depaList.addAll(childList);
			
			for(SysDepa item : childList){
				getChildDepa(item, depaList);
			}
		}
	}

	/**
	 * 根据部门code获取部门信息
	 * @param fjDcode
	 * @return
	 */
	@Override
	public SysDepa getDepaByCode(String fjDcode) {
		return sysDepaDao.getDepaByCode(fjDcode);
	}

	/**
	 * 根据部门类型获取部门列表信息
	 * @param string
	 * @return
	 */
	@Override
	public List<SysDepa> getDepaListByLevel(String type) {
		return sysDepaDao.getDepaListByType(type);
	}
	/**
	 * 得到部门map(部门名称,ID)
	 * @return
	 */
	@Override
	public Map<String, Integer> getDepaMap() {
		Map<String, Integer> map=new  HashMap<String, Integer>();
		for (SysDepa s : sysDepaDao.depaList()) {
			map.put(s.getDepaName(), s.getDepaId());
		}
		
		return map;
	}

	@Override
	public List<SysDepa> getDeparBySort() {
		// TODO Auto-generated method stub
			List<SysDepa> list=sysDepaDao.getDeparBySort();
			return list;
	}

	@Override
	public String getPid(Integer deparId) {
		// TODO Auto-generated method stub
		try {
			//递归取出子部门
			List<SysDepa> deparSonList = new ArrayList<SysDepa>();
			getDeparSonIdByPId(deparSonList,deparId,lenth);
			if (lenth==0) {
				
				lenth=0;
				return "noFloor";
			}
			else if (lenth==1) {
				lenth=0;
				return "oneFloor";
			}
			else if (lenth==2) {
				lenth=0;
				return "twoFloor";
			}
			else if(lenth==3){
				lenth=0;
				return "threeFloor";
			}
			else {
				lenth=0;
				return "error";
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "error";
		}

	}
	
	/**
	 * 递归查询下级部门
	 * @param sysDeparlist
	 * @param deparId
	 */
	public void getDeparSonIdByPId(List<SysDepa> deparSonList,
			Integer deparId,Integer i){
		List<SysDepa> childList = sysDepaDao.listDepaByPId(deparId);
		if(childList != null && !childList.isEmpty()){
			deparSonList.addAll(childList);
			i=i+1;
			lenth=i;
			for(SysDepa item : childList){
				getDeparSonIdByPId(deparSonList,item.getDepaId(),i);
			}
		}
	}

}
